package de.fhd.jfadet;

/* Copyright (c) 2002 by Konrad Rzeszutek and modified by Hans-Joerg Wiehoff
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 */
import java.lang.*;
import java.io.*;


public class TestFaceRecognition implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static void main(String args[]) {

		if (args.length != 2) {
			prUsage();
			System.exit(0);
		}

		String dirString = args[0];
		String fileString = args[1];
		
		String dir1 = System.getProperty("user.dir") + "\\WebContent\\images\\dbImages\\AllDbImage";
		String file1 = System.getProperty("user.dir")+ "\\WebContent\\images\\dbImages\\CurrentImage\\2_1.jpg";
		
		
//		// delete sub-directories created by SVN for test reasons
//		File directory = new File(dir1+"\\.svn");
//    	//make sure directory exists
//    	if(!directory.exists()){
//           System.out.println("Directory does not exist.");
//           //System.exit(0);
//        }else{
//           try{
//               delete(directory);
//           }catch(IOException e){
//               e.printStackTrace();
//               System.exit(0);
//           }
//        }
//    	System.out.println("Done");
    	
    	
    	//  new instance of EigenFaceCreator 
		try {
			EigenFaceCreator creator = new EigenFaceCreator();

//			String packageName = creator.getClass().getPackage().getName();
//          System.out.println("Package Name = " + packageName);
			
			// creator.USE_CACHE = -1;
			System.out.println("Constructing face-spaces from " + dir1 + " ...");
			creator.readFaceBundles(dir1);

			System.out.println("Comparing " + file1 + " ...");
			String result = creator.checkAgainst(file1);

			System.out.println("Most closly reseambling: " + result + " with "
					+ creator.DISTANCE + " distance.");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	static void prUsage() {
		System.err
				.println("Usage: java TestFaceRecognition <directory of training images> <image to test against>");
	}

	public static void delete(File file)
	throws IOException{

	if(file.isDirectory()){

		//directory is empty, then delete it
		if(file.list().length==0){

		   file.delete();
		   System.out.println("Directory is deleted : " 
                                             + file.getAbsolutePath());

		}else{

		   //list all the directory contents
    	   String files[] = file.list();

    	   for (String temp : files) {
    	      //construct the file structure
    	      File fileDelete = new File(file, temp);

    	      //recursive delete
    	     delete(fileDelete);
    	   }

    	   //check the directory again, if empty then delete it
    	   if(file.list().length==0){
       	     file.delete();
    	     System.out.println("Directory is deleted : " 
                                              + file.getAbsolutePath());
    	   }
		}

	}else{
		//if file, then delete it
		file.delete();
		System.out.println("File is deleted : " + file.getAbsolutePath());
	}
}

	
	
}

